package cn.wolfcode.web.controller;


import cn.wolfcode.transfer.domain.${capitalize};
import cn.wolfcode.transfer.service.I${capitalize}Service;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/${uncapitalize}")
public class ${capitalize}Controller {

    @Autowired
    private I${capitalize}Service ${uncapitalize}Service;


    @RequiredPermission(name="${chinese}页面",expression="${uncapitalize}:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<${capitalize}> pageInfo = ${uncapitalize}Service.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "${uncapitalize}/list";
    }


    @RequiredPermission(name="${chinese}删除",expression="${uncapitalize}:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           ${uncapitalize}Service.delete(id);
        }
        return "redirect:/${uncapitalize}/list";
    }


    @RequiredPermission(name="${chinese}新增/编辑",expression="${uncapitalize}:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(${capitalize} ${uncapitalize}) {
        if (${uncapitalize}.getId() == null) {
            ${uncapitalize}Service.save(${uncapitalize});
        } else {
            ${uncapitalize}Service.update(${uncapitalize});
        }
        return "redirect:/${uncapitalize}/list";
    }


    @RequiredPermission(name="${chinese}新增/编辑",expression="${uncapitalize}:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("${uncapitalize}",${uncapitalize}Service.get(id));
        }
        return "${uncapitalize}/input";
    }
}
